﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    class ConfigurationNodeCollection : IConfigurationNodeCollection
    {
        int index = -1;

        List<IConfigurationNode> list = null;

        public ConfigurationNodeCollection(List<IConfigurationNode> list)
        {
            this.list = list;
        }

        public void Add(IConfigurationNode node)
        {
            if (node == null)
                throw new Exception("missing node.");

            string nodename = node.NodeName;

            if (string.IsNullOrEmpty(nodename))
                throw new Exception("missing id in node.");

            nodename = nodename.Trim().ToUpper();

            if (Contains(nodename))
                throw new Exception("node id already in nodes, id = " + nodename);

            list.Add(node);
        }

        public bool Contains(string nodeName)
        {
            if (string.IsNullOrEmpty(nodeName))
                return false;

            foreach (IConfigurationNode node in list)
            {
                if (string.Equals(node.NodeName, nodeName, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        public IConfigurationNode this[int index]
        {
            get
            {
                if (index < 0 || index > list.Count - 1)
                    throw new Exception("index exceed the list.");

                return list[index];
            }
        }

        public IConfigurationNode this[string nodeName]
        {
            get
            {
                if (string.IsNullOrEmpty(nodeName))
                    throw new Exception("missing input nodeName");

                nodeName = nodeName.Trim().ToUpper();

                foreach (IConfigurationNode node in list)
                {
                    if (string.Equals(node.NodeName, nodeName, StringComparison.OrdinalIgnoreCase))
                        return node;
                }

                throw new Exception("do not contain node in collection, nodename = " + nodeName);
            }
        }

        public override string ToString()
        {
            return "list.count = " + list.Count;
        }

        public int Count
        {
            get
            {
                return list.Count;
            }
        }


        #region IEnumerable<IConfigurationNode> 成员

        public IEnumerator<IConfigurationNode> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<IConfigurationNode> 成员

        public IConfigurationNode Current
        {
            get
            {
                return this[index];
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Reset();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return this[index];
            }
        }

        public bool MoveNext()
        {
            if (index >= list.Count - 1)
                return false;

            index++;

            return true;
        }

        public void Reset()
        {
            index = -1;
        }

        #endregion
    }
}
